package cn.com.huzo.webapp.controller.inventory;

import cn.com.huzo.model.entities.inventory.StockOutHeaderEntity;
import cn.com.huzo.model.entities.inventory.StockOutLinesEntity;
import cn.com.huzo.model.entities.master.StockReasonEntity;
import cn.com.huzo.service.inventory.StockOutHeaderManager;
import cn.com.huzo.service.inventory.StockOutLinesManager;
import cn.com.huzo.service.master.StockReasonManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 领料出库单画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/inventory/stock_out")
public class StockOutController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "StockOutHeaderManager")
    private StockOutHeaderManager _manager_header;

    @Resource(name = "StockOutLinesManager")
    private StockOutLinesManager _manager_lines;

    @Resource(name = "StockReasonManager")
    private StockReasonManager _manager_stock_reason;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到领料出库单一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("inventory.stock_out.list.def");
        return view;
    }

    /**
     * 跳转领料出库单编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param stockOutId 领料出库单ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{stockOutId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "stockOutId") Long stockOutId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 得到入出库原因
        List<StockReasonEntity> stockReasonList = _manager_stock_reason.fetchAll(-1);
        view.addObject("StockReasonList", stockReasonList);
        view.addObject("action", action);
        view.addObject("stockOutId", stockOutId);
        view.setViewName("inventory.stock_out.edit.def");
        return view;
    }

    /**
     * 领料出库单选择画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/choice.html")
    public ModelAndView redirectChoice(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.inventory.stock_out.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("BillNo", _manager_header.fetchNewBillNo());
        return _result;
    }

    /**
     * 根据标识取得领料出库单数据。
     *
     * @param criterion 领料出库单标识
     * @return 领料出库单数据（包括主表和子表）。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByHeaderKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"));
    }

    /**
     * 根据条件取得领料出库单信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 领料出库单信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 新增领料出库单信息数据。
     * <p>
     * @param instance 要增加的数据。
     * @return 新增记录主键、明细
     * @throws Exception
     */
    @RequestMapping(value = "/header/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> insert(@RequestBody final StockOutHeaderEntity instance) throws Exception {
        return _manager_header.save(instance);
    }

    /**
     * 保存领料出库单信息数据。
     * <p>
     * @param instance 要保存的领料出库单信息对象。
     * @return 影响行数
     * @throws Exception
     */
    @RequestMapping(value = "/header/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer update(@RequestBody final StockOutHeaderEntity instance) throws Exception {
        return _manager_header.update(instance);
    }

    /**
     * 删除领料出库单信息数据。
     *
     * @param criterion 要删除的数据。
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="领料出库单明细数据处理部分">
    /**
     * 根据条件取得明细数据。
     * <p>
     * @param headerId 出库标识
     * @return 原辅料出库明细
     * @throws Exception
     */
    @RequestMapping(value = "/fetchLinesByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<StockOutLinesEntity> fetchLinesByHeaderKey(@RequestBody final Long headerId) throws Exception {
        return _manager_lines.fetchLinesByHeaderKey(headerId);
    }

    /**
     * 补充领料出库明细数据。
     *
     * @param criterion 引入的领料单id等
     * @return 原辅料出库明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/supplement.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<StockOutLinesEntity> supplement(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.supplement(MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "RsHeaderId"),
                MapUtils.getLong(criterion, "UserId"));
    }

    /**
     * 更新原辅料出库明细
     * <p>
     * @param itemList 要更新的数据
     * @return 更新的记录数.
     * @throws Exception
     */
    @RequestMapping(value = "/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchUpdate(@RequestBody final List<StockOutLinesEntity> itemList) throws Exception {
        return _manager_lines.batchUpdate(itemList);
    }

    /**
     * 删除领料出库单明细数据。
     *
     * @param criterion 要删除的领料出库单明细
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }

    /**
     * 批量删除原辅料出库明细数据。
     *
     * @param itemList 要删除的原辅料出库数据
     * @return 原辅料出库明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLines(@RequestBody final List<StockOutLinesEntity> itemList) throws Exception {
        return _manager_lines.batchDelete(itemList);
    }
    // </editor-fold>
}
